package com.ww.hadoop.mr.topn_mapjoin;

import lombok.SneakyThrows;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class TopN {
    @SneakyThrows
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "root");


        Configuration conf = new Configuration(true);
        //会拉取hdfs上得文件,在本地运行代码
        //conf.set("mapreduce.framework.name","local");
        conf.set("mapreduce.app-submission.cross-platform","true");

        Job job = Job.getInstance(conf);
        job.setJarByClass(TopN.class);
        job.setJobName("TopN");
        job.setJar("D:\\me\\study\\code\\idea\\try-hadoop\\target\\try-hadoop-1.0-SNAPSHOT.jar");

        //客户端规划的时候讲join的右表cache到mapTask出现的节点上
        job.addCacheFile(new Path("/data/topn/dict/dict.txt").toUri());


        //设置文件输入路径
        TextInputFormat.addInputPath(job,new Path("/data/topn/input"));
        Path output = new Path("/data/topn/outputjoin");
        //如果存在就删除目录
        if(output.getFileSystem(conf).exists(output))  output.getFileSystem(conf).delete(output,true);
        //设置文件输出路劲
        TextOutputFormat.setOutputPath(job,output);

        //map
        job.setMapperClass(TMapper.class);
        job.setMapOutputKeyClass(TKey.class);
        job.setMapOutputValueClass(IntWritable.class);

        //分区器
        job.setPartitionerClass(TPartitioner.class);

        //排序比较器
        job.setSortComparatorClass(TSortComparator.class);

        //groupingComparator
        job.setGroupingComparatorClass(TGroupingComparator.class);

        //reduce
        job.setReducerClass(TReducer.class);


        //完成后退出
        job.waitForCompletion(true);

        //Thread.sleep(1000000);

    }
}
